package com.shujia.day12;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/*
    Collection集合存储自定义对象
    1、创建集合对象
    2、创建元素对象
    3、将元素添加到集合中
    4、遍历集合
 */
public class CollectionDemo5 {
    public static void main(String[] args) {
        //1、创建集合对象
        Collection c1 = new ArrayList();

        //2、创建元素对象
        Student s1 = new Student("李刚", 15);
        Student s2 = new Student("江川", 11);
        Student s3 = new Student("钱志强", 17);
        Student s4 = new Student("祝帅", 15);

        // 3、将元素添加到集合中
        c1.add(s1);
        c1.add(s2);
        c1.add(s3);
        c1.add(s4);

        // 4、遍历集合
        //方式1：转数组遍历
//        Object[] objects = c1.toArray();
//        for(int i=0;i<objects.length;i++){
//            Object obj = objects[i];
//            Student s = (Student) obj;
//            System.out.println(s.getName()+"-"+s.getAge());
//        }

        //方式2：迭代器遍历
        Iterator iterator = c1.iterator();
        while (iterator.hasNext()){
            Object obj = iterator.next();
            Student s = (Student) obj;
            System.out.println(s.getName()+"-"+s.getAge());
        }
    }
}
